﻿// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using NUnit.Framework;
using ValidationAspects;
using ValidationAspects.Sdk;

namespace TestValidationAspects
{
	public class MockValidatorFactory<T> : ValidatorFactoryBase<T>
	{
		public bool Fail { get; set; }
		public object Value { get; set; }
		public IValidationContext Context { get; set; }

		public void TestState(object value, IValidationContext context)
		{
			Assert.AreSame(value, Value);
			Assert.AreSame(context, Context);
		}

		public void Reset()
		{
			Fail = false;
			Value = null;
			Context = null;
		}

		protected override Action<T, IValidationContext> ValidateAction
		{
			get
			{
				return (v, c) =>
				       {
				       	Value = v;
				       	Context = c;

				       	if (Fail)
				       		throw new ValidationException("failed");
				       };
			}
		}
	}
}